/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.movieSystem.view;

import java.awt.*;
import java.io.IOException;
import java.util.HashMap;

import javax.swing.JOptionPane;

import com.movieSystem.model.UserInfo;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.stage.Stage;
import tcpFirmware.*;


/**
 *
 * @author DELL-PC
 */
public class FrmLogin extends javax.swing.JFrame {

	public FrmLogin self;
    public FrmLogin() {
        initComponents();
        self=this;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

//        new JFXPanel();
        Platform.setImplicitExit(false);
        Label1 = new javax.swing.JLabel();
        Label2 = new javax.swing.JLabel();
        TextFUserName = new javax.swing.JTextField();
        BtnLogin = new javax.swing.JButton();
        BtnReset = new javax.swing.JButton();
        TextPassword = new javax.swing.JPasswordField();
        BtnLogon = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("登录");

        Label1.setText("用户名");

        Label2.setText("密  码");

        TextFUserName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TextFUserNameActionPerformed(evt);
            }
        });

        BtnLogin.setText("登录");
        BtnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnLoginActionPerformed(evt);
            }
        });

        BtnReset.setText("清空");
        BtnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnResetActionPerformed(evt);
            }
        });

        TextPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TextPasswordActionPerformed(evt);
            }
        });

        BtnLogon.setText("注册");
        BtnLogon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnLogonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(Label2)
                            .addComponent(Label1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(TextFUserName)
                            .addComponent(TextPassword)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(BtnLogin)
                        .addGap(18, 18, 18)
                        .addComponent(BtnLogon)
                        .addGap(18, 18, 18)
                        .addComponent(BtnReset)))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Label1)
                    .addComponent(TextFUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Label2)
                    .addComponent(TextPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(45, 45, 45)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BtnLogin)
                    .addComponent(BtnLogon)
                    .addComponent(BtnReset))
                .addContainerGap(53, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);

    }// </editor-fold>//GEN-END:initComponents

    public static boolean javaFxFirst=true;
    private void BtnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnLoginActionPerformed
             
            //获取用户输入的账号和密码
            String userName=TextFUserName.getText();
            //获取密码的字符数组
            char[] pwd=TextPassword.getPassword();;
            //根据字符数组创建字符串
            String password=new String(pwd);

            //验证输入是否为空
            if(userName.length()>0 && password.length()>0){
                //访问数据库验证账户和密码是否正确(在DBCon类中)
                
                    //登录成功
                    
                try {
					OKClient okClient = new OKClient(new Response() {
						
						@Override
						public void onResponse(HashMap<String, String> header, byte[] dataBytes) {
							// TODO Auto-generated method stub
							HashMap<String, String> data = OKClient.dealWithForm(dataBytes);
							if(header.get("登录状态").equals("登录成功")) {
								JOptionPane.showMessageDialog(null,"登录成功","系统提示",JOptionPane.INFORMATION_MESSAGE);
					        //打开主界面
                                UserInfo.setUsername(userName);
                                UserInfo.setPassword(password);
                                UserInfo.setIsAdmin(header.get("身份"));
                                dispose();

                                if(javaFxFirst)
                                    MovieTime.open();
                                else {
                                    Platform.runLater(()->new MovieTime().start(new Stage()));
                                }
					        //关闭当前的登录窗口

							}else {
                                JOptionPane.showMessageDialog(null,"登录失败","系统提示",JOptionPane.INFORMATION_MESSAGE);
                            }
						}
					});
					okClient.setType("text/formData").setRoute("login").setData("username", userName).setData("password", password);
					okClient.sendTo();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                
                    //JOptionPane.showMessageDialog(null,"登录失败，用户名或密码错误","系统警告",JOptionPane.WARNING_MESSAGE);
                
            
                //提示对话框
                //JOptionPane.showMessageDialog(null,"账号和密码输入不能为空","系统警告",JOptionPane.WARNING_MESSAGE);
            
            }  
    }//GEN-LAST:event_BtnLoginActionPerformed

    private void TextPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TextPasswordActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TextPasswordActionPerformed

    private void BtnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnResetActionPerformed
        TextFUserName.setText("");
        TextPassword.setText("");
    }//GEN-LAST:event_BtnResetActionPerformed

    private void TextFUserNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TextFUserNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TextFUserNameActionPerformed

    private void BtnLogonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnLogonActionPerformed
        // TODO add your handling code here:
        RegisterView rs = new RegisterView();
        rs.setVisible(true);
    }//GEN-LAST:event_BtnLogonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmLogin().setVisible(true);
            }
        });

    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnLogin;
    private javax.swing.JButton BtnLogon;
    private javax.swing.JButton BtnReset;
    private javax.swing.JLabel Label1;
    private javax.swing.JLabel Label2;
    private javax.swing.JTextField TextFUserName;
    private javax.swing.JPasswordField TextPassword;
    // End of variables declaration//GEN-END:variables
}
